package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.*;
import cn.itcast.domain.system.User;
import cn.itcast.service.cargo.ShippingControllerService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/cargo/shipping")
@Slf4j
public class ShippingController extends BaseController {

    @Reference
    private ShippingControllerService shippingControllerService;


    /**
     * 1. 委托管理列表
     */
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize) {
        log.info("执行委托列表查询开始...");
        // 构造条件对象
        ShippingOrderExample shippingOrderExample = new ShippingOrderExample();
        // 根据create_time降序排序
        shippingOrderExample.setOrderByClause("create_time desc");
        // 获取条件的集合
        ShippingOrderExample.Criteria criteria = shippingOrderExample.createCriteria();
        // 添加条件
        criteria.andShipperEqualTo(getLoginCompanyId());

        PageInfo<ShippingOrder> pageInfo = shippingControllerService.findByPage(shippingOrderExample, pageNum, pageSize);
        request.setAttribute("pageInfo", pageInfo);
        return "cargo/shipping/shipping-list";
    }

    /**
     * 2.进入新增页面
     */
    @RequestMapping("/toAdd")
    public String toAdd(Model model){
        return "cargo/shipping/shipping-add";
    }

    /**
     * 3、新增委托单
     */
    @RequestMapping("/edit")
    public String edit(ShippingOrder shippingOrder){
/*        shippingOrder.setPackingIds(getLoginCompanyId());
        if (StringUtils.isEmpty(shippingOrder.getId())){
            shippingControllerService.save(shippingOrder);
        }else{
            shippingControllerService.update(shippingOrder);
        }*/
        return "redirect:/cargo/shipping/list";
    }

    /**
     * 4、删除委托单
     */
    @RequestMapping("/delete")
    public Map<String,Integer> delete(String id){
        //根据委托单查询状态码
        ShippingOrder shippingOrder = shippingControllerService.findById(id);
        //获取状态码
        Integer stateId = shippingOrder.getState();
        //定义返回结果集
        Map<String, Integer> map = new HashMap<>();
        //判断状态，为0则删除
        if ( stateId ==0){
            map.put("massage",0);
            shippingControllerService.delete(id);
        }else {
            map.put("massage",1);
        }
        return map;
    }
}
